package com.xgk.boot.module.core.controller.admin.price;

import com.xgk.boot.framework.common.pojo.CommonResult;
import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.common.util.object.BeanUtils;
import com.xgk.boot.module.core.controller.admin.price.vo.*;
import com.xgk.boot.module.core.dal.entity.price.LgExtrasDictDO;
import com.xgk.boot.module.core.dal.entity.price.PriceRuleDO;
import com.xgk.boot.module.core.service.price.LgExtrasDictServiceImpl;
import com.xgk.boot.module.core.service.price.PriceRuleServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.xgk.boot.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 扣费规则")
@RestController("adminPriceRuleController")
@RequestMapping("/system/price")
public class PriceRuleController {

    @Resource
    private PriceRuleServiceImpl priceRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建扣费规则")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<Boolean> createPriceRule(@Valid @RequestBody PriceRuleSaveVO createReqVO) {
        return success(priceRuleService.savePriceRule(createReqVO));
    }

    @PutMapping("/update/{id}")
    @Operation(summary = "修改扣费规则")
    @Parameter(name = "id", description = "扣费规则id", required = true, example = "1934923143484133377")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<Boolean> updatePriceRule(@PathVariable("id") Long id,@Valid @RequestBody PriceRuleSaveVO updateReqVO) {
        return success(priceRuleService.updatePriceRule(updateReqVO));
    }


    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除扣费规则")
    @Parameter(name = "id", description = "扣费规则id", required= true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<Boolean> deletePriceRule(@PathVariable("id") Long id) {
        return success(priceRuleService.deletePriceRule(id));
    }


    @GetMapping("/get/{id}")
    @Operation(summary = "获取扣费规则信息")
    @Parameter(name = "id", description = "扣费规则id", required = true, example = "1934923143484133377")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<PriceRuleVO> getPriceRuleById(@PathVariable("id") Long id) {
        PriceRuleDO priceRuleById = priceRuleService.getPriceRuleById(id);
        return success(BeanUtils.toBean(priceRuleById,PriceRuleVO.class));
    }

    @PostMapping("/page_list")
    @Operation(summary = "分页获得扣费规则列表")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<PageResult<PriceRuleVO>> pagePriceRules(@Valid @RequestBody PriceRuleReqVO pageReqVO) {
        PageResult<PriceRuleDO> customerDOPageResult = priceRuleService.pageList(pageReqVO);
        PageResult<PriceRuleVO> bean = BeanUtils.toBean(customerDOPageResult, PriceRuleVO.class);
        return CommonResult.success(bean);
    }

    @PostMapping("/list/{type}")
    @Operation(summary = "获得扣费规则列表")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<List<PriceRuleVO>> listPriceRules(@PathVariable("type") String type,@Valid @RequestBody PriceRuleReqVO pageReqVO) {
        List<PriceRuleDO> priceRuleDOS = priceRuleService.listPriceRules(type, pageReqVO);
        List<PriceRuleVO> bean = BeanUtils.toBean(priceRuleDOS, PriceRuleVO.class);
        return CommonResult.success(bean);
    }

}
